package com.tommy.jiami.ui;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.tommy.jiami.R;
import com.tommy.jiami.ui.dialog.LockDialog;
import com.tommy.jiami.utils.Constants;
import com.tommy.jiami.utils.Crypt;
import com.tommy.jiami.utils.Media;
import com.tommy.jiami.utils.task.CryptMediaTask;
import com.tommy.jiami.view.MediaView;

import java.util.ArrayList;
import java.util.List;

public class MediaActivity extends AppCompatActivity
		implements AbsListView.MultiChoiceModeListener
{
	ViewPager viewPager;
	List<MediaView> medias;
	BroadcastReceiver receiver=new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent)
		{
			medias.get(intent.getIntExtra("index",0)).reset();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_media);
		Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar) ;
		setSupportActionBar(toolbar);
		initMediaViews();
		initViewPager();
		initActionBar();
	}

	private void initActionBar()
	{
		TabLayout tabLayout=(TabLayout)findViewById(R.id.tab_layout);
		tabLayout.setupWithViewPager(viewPager);
	}


	private void initMediaViews()
	{
		medias=new ArrayList<MediaView>();
		MediaView picture,audio,video;
		picture=new MediaView(this,MediaView.TYPE_PICTURE);
		audio=new MediaView(this,MediaView.TYPE_AUDIO);
		video=new MediaView(this,MediaView.TYPE_VIDEO);
		medias.add(picture);
		medias.add(audio);
		medias.add(video);
		picture.init();
		for(MediaView media:medias){
			media.setChoiceMode(MediaView.CHOICE_MODE_MULTIPLE_MODAL);
			media.setMultiChoiceModeListener(this);
		}
	}

	private void initViewPager()
	{
		viewPager=(ViewPager)findViewById(R.id.audio_viewpager);
		viewPager.setAdapter(new MyPagerAdapter());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(1,1,1,"刷新")
				.setIcon(R.drawable.menu_refresh)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		menu.add(2,2,2,"查看已加密文件")
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId()){
			case 1:
				medias.get(viewPager.getCurrentItem()).reset();
				break;
			case 2:
				LockDialog dialog=new LockDialog();
				dialog.setOnLockSuccessListener(new LockDialog.OnLockSuccessListener(){
					@Override
					public void onSuccess()
					{
						startActivity(new Intent(MediaActivity.this,EncryptedActivity.class));
					}
				});
				dialog.show(getFragmentManager(),"dialog");
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateActionMode(ActionMode mode, Menu menu)
	{
		menu.add(1,1,1,"加密").setIcon(R.drawable.menu_add)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return true;
	}

	@Override
	public boolean onActionItemClicked(ActionMode mode, MenuItem menu)
	{
		int index=viewPager.getCurrentItem();
		List<Media> mediaList=new ArrayList<Media>();
		for(long id:medias.get(index).getCheckedItemIds()){
			mediaList.add(medias.get(index).getMedia((int)id));
		}
		CryptMediaTask task=new CryptMediaTask(this, Crypt.TYPE_ENCRYPT,index);
		task.execute(mediaList.toArray(new Media[mediaList.size()]));
		mode.finish();
		return false;
	}


	@Override
	protected void onStart()
	{
		IntentFilter filter=new IntentFilter();
		filter.addAction(Constants.ACTION_CRYPT_OK);
		registerReceiver(receiver,filter);
		super.onStart();
	}

	@Override
	protected void onStop()
	{
		unregisterReceiver(receiver);
		super.onStop();
	}

	class MyPagerAdapter extends PagerAdapter
	{

		@Override
		public Object instantiateItem(ViewGroup container, int position)
		{
			MediaView grid=medias.get(position);
			container.addView(grid);
			return grid;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object)
		{
			container.removeView(medias.get(position));
		}

		@Override
		public int getCount()
		{
			return medias.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object)
		{
			return view==object;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position){
				case 0:
					return "图片";
				case 1:
					return "音频";
				case 2:
					return "视频";
			}
			return super.getPageTitle(position);
		}
	}

	@Override
	public boolean onPrepareActionMode(ActionMode p1, Menu p2){return false;}
	@Override
	public void onDestroyActionMode(ActionMode p1){}
	@Override
	public void onItemCheckedStateChanged(ActionMode p1, int p2, long p3, boolean p4){}

}
